#ifndef _CRT_SECURE_NO_WARNINGS
	#define _CRT_SECURE_NO_WARNINGS // Deal with warnings in glui.h
#endif

#include <GL\glui.h>
#include "Functions.h"
#include "walls.h"
#include "math.h"
#include "Textures.h"

#define NQUAD_STRIPS 50

GLfloat mat_floor_ambient[4] = {0.55, 0.55, 0.55, 1.0};
GLfloat mat_floor_diffuse[4] = {0.85, 0.85, 0.85, 1.0};
GLfloat mat_floor_specular[4] = {0.3, 0.3,0.3, 1.0};
GLfloat mat_floor_shininess[1] = {128};

// Floor evaluator declarations
GLfloat ctrlpoints[4][3] = {	{  ROOMX, 0.0, ROOMZ},
								{ -ROOMX, 0.0, ROOMZ},
								{  ROOMX, 0.0, -ROOMZ},
								{ -ROOMX, 0.0, -ROOMZ} };

GLfloat nrmlcompon[4][3] = {	{  0.0, 1.0, 0.0},
								{  0.0, 1.0, 0.0},    
								{  0.0, 1.0, 0.0},
								{  0.0, 1.0, 0.0} };

GLfloat colorpoints[4][4] = {	{ 0.0, 0.2, 0.2, 0},
								{ 0.0, 0.0, 0.2, 0}, 
								{ 0.0, 0.2, 0.0, 0},
								{ 0.2, 0.0, 0.0, 0} };

GLfloat textpoints[4][2] = {	{ 0.0, 0.0},
								{ 0.0, 1.0}, 
								{ 1.0, 0.0},
								{ 1.0, 1.0} };


void draw_door_wall() {
	glPushMatrix();
		glTranslated(0.0, 0.0, DOOR_Z);
		
		drawStripRect(-ROOMX, ROOMX, DOOR_DIM_Y, ROOMY, NQUAD_STRIPS);
		drawStripRect(DOOR_DIM_X, ROOMX, DOOR_Y, DOOR_DIM_Y, NQUAD_STRIPS);
		drawStripRect(-ROOMX, DOOR_X, DOOR_Y, DOOR_DIM_Y, NQUAD_STRIPS);

	glPopMatrix();
}

void draw_window_wall() {
	glPushMatrix();
		glTranslated(-ROOMX, 0.0, 0.0);
		glRotated(90, 0.0, 1.0, 0.0);
		drawStripRect(-ROOMZ, ROOMZ, WINDOW_DIM_Y, ROOMY, NQUAD_STRIPS);
		drawStripRect(-ROOMZ, -WINDOW_DIM_Z,  (ROOMY - WINDOW_DIM_Y) / 2, WINDOW_DIM_Y, NQUAD_STRIPS);
		drawStripRect(WINDOW_DIM_Z, ROOMZ,  (ROOMY - WINDOW_DIM_Y) / 2, WINDOW_DIM_Y, NQUAD_STRIPS);
		drawStripRect(-ROOMZ, ROOMZ, 0.0, ROOMY - WINDOW_DIM_Y, NQUAD_STRIPS);
	glPopMatrix();
}

void draw_viewer_wall() {
	glPushMatrix();
		glTranslated(0.0, 0.0, ROOMZ);
		glRotated(180, 0.0, 1.0, 0.0);
		drawStripRect(-ROOMX, ROOMX, 0.0, ROOMY, NQUAD_STRIPS);
	glPopMatrix();
}

float curved_array[WALL_SLICES + 2];

void fill_curved_array() {
	for(int i = 0; i <= WALL_SLICES + 2; i++) 
		curved_array[i] = pow(sin(i*9*PI/180)*2, 3);
}

void draw_curved_wall() {
	float coordZ = ROOMZ;
	float dz = 2*ROOMZ/WALL_SLICES;
	float coordX = ROOMX - pow(sin(90*PI/180)*2, 3);
	float xStep = 0;
	float nx = 0, ny = 0, nz = 0;

	float x[] = {0, 0};
	float y[] = {0, 0};
	float z[] = {0, 0};

	glEnable(GL_COLOR_MATERIAL);

	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT,   mat_floor_ambient);
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,   mat_floor_diffuse);
	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,  mat_floor_specular);
	glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_floor_shininess);

	for(int i = 1; i <= WALL_SLICES + 2; i++, coordZ -= dz) {			
			
		xStep = coordX + curved_array[i];
			
		if (!(i%2) && i!=0) {
			x[1] = xStep;
			y[1] = ROOMY;
			z[1] = coordZ;

			newellMethod(&nx, &ny, &nz, 1, x, y, z); 
			x[0] = x[1];
			y[0] = y[1];
			z[0] = z[1];
		} else {
			x[0] = xStep;
			y[0] = ROOMY;
			z[0] = coordZ;
		}

		drawStripRect(xStep, coordX + curved_array[i-1], 0.0, ROOMY, coordZ - dz, coordZ, WALL_SLICES);
	}

	glDisable(GL_COLOR_MATERIAL);
}

void draw_walls() {
	glEnable(GL_COLOR_MATERIAL);
	glColor3f(0.56f, 0.56f, 0.56f);
		draw_door_wall();
		draw_window_wall();
		draw_viewer_wall();
		draw_curved_wall();
	glDisable(GL_COLOR_MATERIAL);
}

void draw_floor() { 

        glEnable(GL_TEXTURE_2D);

			glBindTexture(GL_TEXTURE_2D, FLOOR_TEX_ID);
			
			glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT,   mat_floor_ambient);
			glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,   mat_floor_diffuse);
			glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,  mat_floor_specular);
			glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_floor_shininess);
                
			glMap2f(GL_MAP2_VERTEX_3, 0.0, 1.0, 3, 2, 0.0, 1.0, 6, 2, &ctrlpoints[0][0]);
			glMap2f(GL_MAP2_NORMAL, 0.0, 1.0, 3, 2, 0.0, 1.0, 6, 2, &nrmlcompon[0][0]);
			glMap2f(GL_MAP2_TEXTURE_COORD_2,  0.0, 0.1, 2, 2,  0.0, 0.2, 4, 2,  &textpoints[0][0]);

			glEnable(GL_MAP2_VERTEX_3);
				glEnable(GL_MAP2_NORMAL);
					glEnable(GL_MAP2_TEXTURE_COORD_2);
						glMapGrid2f(FLOOR_SLICES, 0.0, 1.0, FLOOR_SLICES/2, 0.0, 1.0);
						glEvalMesh2(GL_FILL, 0, FLOOR_SLICES, 0, FLOOR_SLICES/2);
					glDisable(GL_MAP2_TEXTURE_COORD_2);
				glDisable(GL_MAP2_NORMAL);
			glDisable(GL_MAP2_VERTEX_3);

        glDisable(GL_TEXTURE_2D);
}